package cn.touna.jss.common.webservice;

import cn.touna.jss.common.util.AxisTwoWayAuthSslSocketFactory;
import org.apache.axis.AxisProperties;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.codehaus.xfire.client.Client;
import org.codehaus.xfire.transport.http.CommonsHttpMessageSender;

import java.net.URL;
import java.util.Map;

/**
 */
public class WebServiceUtil {

    public static Object[] invoke(String url, String name, Object[] params) throws Exception {
        Client client = new Client(new URL(url));
        client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, String.valueOf(30 * 1000));//设置发送的超时限制,单位是毫秒;
        client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true");
        client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true");
        Object[] result = client.invoke(name, params);
        client.close();
        return result;
    }

    public static String invokeHttp(String url, String intfName, Object[] params) throws Exception {
        Service service = new Service();
        Call call = (Call) service.createCall();
        call.setTargetEndpointAddress(new URL(url));
        call.setOperationName(intfName);
        call.setTimeout(30000);
        return (String) call.invoke(params);
    }

    public static String invokeTwoWayAuth(String url, String intfName, Object[] params, Map config) throws Exception {
        Service service = new Service();
        AxisTwoWayAuthSslSocketFactory.initConfig(config);
        AxisProperties.setProperty("axis.socketSecureFactory", AxisTwoWayAuthSslSocketFactory.class.getName());
        Call call = (Call) service.createCall();
        call.setTargetEndpointAddress(new URL(url));
        call.setOperationName(intfName);
        call.setTimeout(30000);
        return (String) call.invoke(params);
    }
}
